package com.own.toyexchange;

/*
 * 用户已发布的玩具列表
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjtu.toyexchange.R;
import com.main.toyexchange.GerenActivity;

public class OwnFabu extends Activity {
	private Button fanhui;
	private ListView listview;
	private LayoutInflater layout;
	private String username;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.own_fabu);
        
        Intent intent = this.getIntent();
		
		username = intent.getStringExtra("username");
		
		//返回的按钮
		fanhui = (Button)findViewById(R.id.own_fabu_back);
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				OwnFabu.this.finish();
			}
		});
        
        findViewById();
		initView();
	}
	
	protected void findViewById() {
		listview = (ListView)this.findViewById(R.id.owntoyList);

		listview.setAdapter(new CatergorAdapter());
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view, int parent,
					long id) {
				
				Intent int2 = new Intent();
				String toy_title = mTitleValues[parent];
				String toy_content = mContentValues[parent];
				Toast.makeText(OwnFabu.this, toy_title, 200).show();
				int2.putExtra("title", toy_title);
				int2.putExtra("content", toy_content);
				int2.putExtra("username", username);
				int2.setClass(OwnFabu.this, OwnToyInfo.class);
				OwnFabu.this.startActivity(int2);
			}
		});
	}

	protected void initView() {

	}
	
	private class CatergorAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			return 0;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressWarnings("null")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = new ViewHolder();
			layout = LayoutInflater.from(OwnFabu.this);
			
			//组装数据
			if(convertView==null){
				convertView = layout.inflate(R.layout.toy_item, null);
				holder.image = (ImageView)convertView.findViewById(R.id.toy_pic);
				holder.title = (TextView)convertView.findViewById(R.id.toy_title);
				holder.content = (TextView)convertView.findViewById(R.id.toy_info);
				//使用tag存储数据
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			holder.image.setImageResource(mImageIds[position]);
			holder.title.setText(mTitleValues[position]);
			holder.content.setText(mContentValues[position]);
			
			return convertView;
		}
	}
	
	
	// 适配显示的图片数组
	private Integer[] mImageIds = {R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic,
			R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic
			 };
	//给照片添加文字显示(Title)
	private String[] mTitleValues = { "玩具名1", "玩具名2", "玩具名3", "玩具名4", "玩具名5",
			"玩具名6", "玩具名7", "玩具名8" };
	
	private String[] mContentValues={"玩具介绍1111111111111111111111111111111111111111111111111111111111111111111111111111", "玩具介绍2", "玩具介绍3", "玩具介绍4", "玩具介绍5", 
			"玩具介绍6", "玩具介绍7", "玩具介绍8"};
			

	 public static class ViewHolder {
			ImageView image;
			TextView title;
			TextView content;
	 }

}
